package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.entity.Comment;
import cn.tedu.weibo.pojo.entity.User;
import cn.tedu.weibo.pojo.vo.CommentVO;
import cn.tedu.weibo.result.JsonResult;
import cn.tedu.weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
@Api(tags = "评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("/insert")
    @ApiOperation("发表评论")
    public JsonResult addComment(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        User user = (User) session.getAttribute("user");
        System.out.println("当前登录用户信息是"+user);
        if (user==null){//未登录
            return new JsonResult(Status.NOT_LOGIN);
        }
        Comment comment = new Comment();
        comment.setContent(commentDTO.getContent());
        comment.setCreated(new Date());
        comment.setUserId(user.getId());
        comment.setBlogId(commentDTO.getWeiboId());
        commentMapper.insert(comment);
        return JsonResult.success();
    }
    @GetMapping("/selectByWeiboId")
    @ApiOperation("查看微博所有评论")
    public JsonResult findAllByBlogId(Integer id){
        System.out.println("blogId="+id);
        return JsonResult.success(commentMapper.findByBlogId(id));
    }
}
